$(document).ready(function()
{
  var p=0;
  var q=0;
  var x=0;
  $("#username").blur(function()
  {
     var username = $("#username").val();
	 if (username == "") 
	 {
	 	$("#ifname").text("您还没名呐！！！");
		$("#ifname").css({"font-size":"20px","color":"red","position":"absolute"});
		p=0;
	 	return;
	 }
	 else
	 {
		 $("#ifname").empty();
	 }
	 
	 for(var i=1;i<=localStorage.length;i++)
     {
	    if(localStorage.getItem(i)!=null)
	    {
			var preuser=localStorage.getItem(i);
			preuser=JSON.parse(preuser);
			if((preuser.username!=null)&&(preuser.username==username))
		    {
			  $("#ifname").text("真不巧重名了");
			  $("#ifname").css({"font-size":"20px","color":"red","position":"absolute"});
			  p=0;
			  return;
		    }
		}
	 }
	 p=1;
	 $("#ifname").empty();
	 return;
  });
  
  $("#useremail").blur(function()
  {
	$("#ifmail").empty();
	var useremail = $("#useremail").val();
	var a = useremail.indexOf("@",0);
	var b = useremail.indexOf(".",0);
	if (useremail == "") 
	{
		$("#ifmail").text("您怎么把邮箱忘啦！！！");
		$("#ifmail").css({"font-size":"20px","color":"red","position":"absolute"});
		q=0;
		return;
	}
	else
	{
		$("#ifmail").empty();
	}
	
	if (a > b&&b!=-1) 
	{
		$("#ifmail").text("您内个.跑@前面啦");
		$("#ifmail").css({"font-size":"20px","color":"red","position":"absolute"});
		q=0;
		return;
	}
	else
	{
		$("#ifmail").empty();
	}
	
	if (useremail.indexOf("@",0) == -1) 
	{
		$("#ifmail").text("哎呦，少了个@");
		$("#ifmail").css({"font-size":"20px","color":"red","position":"absolute"});
		q=0;
		return;
	}
	else
	{
		$("#ifmail").empty();
	}
	
	if (useremail.indexOf(".",0) == -1) 
	{
		$("#ifmail").text("哎呦，少了个.");
		$("#ifmail").css({"font-size":"20px","color":"red","position":"absolute"});
		q=0;
		return;
	}
	else
	{
		$("#ifmail").empty();
	}
	q=1;
	$("#ifmail").empty();
	return;
  });
  
  $("#userpassword").blur(function()
  {
	var userpassword = $("#userpassword").val();
	if (userpassword.length == 0) 
	{
		$("#ifpassword").text("没密码挺吓人");
		$("#ifpassword").css({"font-size":"20px","color":"red","position":"absolute"});
		return;
	}
    else
    {
		$("#ifpassword").empty();
    }
	
	var userpassword2 = $("#userpassword2").val();
	if (userpassword2.length == 0&&userpassword.length!=0) 
	{
		$("#ifpassword2").text("咱还是重复检查一下");
		$("#ifpassword2").css({"font-size":"20px","color":"red","position":"absolute"});
		return;
	}
	else
	{
		$("#ifpassword2").empty();
	}

	$("#ifpassword2").empty();
	if(userword2.length==0)
	{
		return;
	}
    if(userpassword!=userpassword2)
    {
    	$("#ifpassword2").text("这么会儿功夫就忘了");
    	$("#ifpassword2").css({"font-size":"20px","color":"red","position":"absolute"});
    	return;
    }
    else
    {
    	$("#ifpassword2").empty();
    }
  });

  $("#userpassword2").blur(function()
  {
	var userpassword = $("#userpassword").val();
	var userpassword2 = $("#userpassword2").val();
  
	if(userpassword!=userpassword2)
	{
		$("#ifpassword2").text("这么会儿功夫就忘了");
		$("#ifpassword2").css({"font-size":"20px","color":"red","position":"absolute"});
		return;
	}
	else
	{
		$("#ifpassword2").empty();
	}
	$("#ifpassword2").empty();
  });

  $("#register").click(function()
  {
	if(q==0||p==0)
	{
		alert("您没弄完呢");
		return;
	}
	if (!window.localStorage) 
    {
        alert("localstorage好像不能用啊......");
        return;
    } 
    else 
    {
        var username = $("#username").val();
		var useremail = $("#usermail").val();
		var userpassword = $("#userpassword").val();
		var i=localStorage.length;
		var date = new Date();
		var registertime=date.toLocaleString();
		var newuser=
		    {
				username:username,
				useremail:$("#useremail").val(),
				userpassword:userpassword,
				userregistertime:registertime,
				userlogintime:null
			}
		newuser=JSON.stringify(newuser);
		localStorage.setItem(i+1,newuser);
		for(var j=1;j<=3;j++)
		{
			alert("得嘞您，欢迎您加入好人之家再点"+(4-j)+"次跳转");
			setTimeout(1000);
		}
		window.open("login.html");
		window.close("index.html");
    }
  });
  $("#login").click(function()
  {	
  		window.open("login.html");
  		window.close("register.html");
  });
});
